﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using FangDuShi_API.DBUtility;
using System.Data;

namespace FangDuShi_API.DAL
{
    /// <summary>
    /// 地区 数据类
    /// </summary>
    public class District
    {
        public District()
        { }/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FangDuShi_API.Model.District DataRowToModel(DataRow row)
        {
            FangDuShi_API.Model.District model = new FangDuShi_API.Model.District();
            if (row != null)
            {
                if (row["FDistrictNo"] != null)
                {
                    model.FDistrictNo = row["FDistrictNo"].ToString();
                }
                if (row["FParentDistrictNo"] != null)
                {
                    model.FParentDistrictNo = row["FParentDistrictNo"].ToString();
                }
                if (row["FDistrictName"] != null)
                {
                    model.FDistrictName = row["FDistrictName"].ToString();
                }
                if (row["FIsCentral"] != null)
                {
                    model.FIsCentral = row["FIsCentral"].ToString();
                }
                if (row["FQuanPin"] != null)
                {
                    model.FQuanPin = row["FQuanPin"].ToString();
                }
                if (row["FJianPin"] != null)
                {
                    model.FJianPin = row["FJianPin"].ToString();
                }
                if (row["FLevel"] != null && row["FLevel"].ToString() != "")
                {
                    model.FLevel = int.Parse(row["FLevel"].ToString());
                }
                if (row["FHomePage"] != null)
                {
                    model.FHomePage = row["FHomePage"].ToString();
                }
                if (row["FIsWebStart"] != null)
                {
                    model.FIsWebStart = row["FIsWebStart"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得省级数据列表
        /// </summary>
        public DataSet GetProvinceList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FDistrictNo,FParentDistrictNo,FDistrictName,FIsCentral,FQuanPin,FJianPin,FLevel,FHomePage,FIsWebStart ");
            strSql.Append(" FROM t_District ");
            strSql.Append(" WHERE ((LEN(FDistrictNo)=2 AND FParentDistrictNo IS NOT NULL ) OR  LEN(FDistrictNo)=4)");
            strSql.Append(" ORDER BY FDistrictNo");
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得某个地方的市级数据列表
        /// </summary>
        public DataSet GetCityList(string parentDistrictNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FDistrictNo,FParentDistrictNo,FDistrictName,FIsCentral,FQuanPin,FJianPin,FLevel,FHomePage,FIsWebStart ");
            strSql.Append(" FROM t_District ");
            strSql.Append(" WHERE FParentDistrictNo=@FParentDistrictNo");
            strSql.Append(" ORDER BY FDistrictName");

            SqlParameter[] parameters = {
					new SqlParameter("@FParentDistrictNo", SqlDbType.NVarChar,40)
			};
            parameters[0].Value = parentDistrictNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        

    }
}
